<?php
namespace other\orders;
use other\ShopingConn;
use other\Table;

/**
 * 修改订单的dhour值
 */
class Dhour
{
    /**
     * 处理    Abnormal
     * @return void
     * @throws \think\Exception
     */
    public function do()
    {

        // order 订单表的 dhour字段维护
        ShopingConn::getConn()->table(Table::Table_Order)
            ->field("id,DATE_FORMAT(FROM_UNIXTIME(orders.created_at-57600), '%y%m%d%H') AS cdate,dhour")
            ->where('dhour',0)
            ->where('created_at','>',0)
            ->chunk(5000,function ($datas){
                if ($datas){
                    foreach ($datas as $data){
                        $id = $data['id'];
                        $dhour = $data['cdate'];
                        if ($dhour){
                            ShopingConn::getConn()->table(Table::Table_Order)
                                ->where('id',$id)->update(compact("dhour"));
                        }
                    }
                }
            });


        // ip_log 表的 dhour字段的维护
        ShopingConn::getConn()->table(Table::Table_Ip_Log)
            ->field("id,created_at,dhour")
            ->where('is_run_dhour','0')
            ->where('created_at','>',0)
            ->chunk(5000,function ($datas){
                if ($datas){
                    var_dump(1);
                    foreach ($datas as $data){
                        $id = $data['id'];
                        $dhour = date('ymdH',strtotime($data['created_at']));
                        if ($dhour)
                        {
                            $uRow = [
                                'dhour' => $dhour,
                                'is_run_dhour' => '1',
                            ];
                            ShopingConn::getConn()->table(Table::Table_Ip_Log)->where('id', $id)->update($uRow);
                        }
                    }
                }
            });
    }
}
